<?php
class controller_TagController {
    private $view;
    private $args;

    public function setView(View $view) {
        $this->view = $view;
        return $this;
    }

    public function setArgs($args = null) {
        if(null != $args) {
            $this->args = $args;
        }
        return $this;
    }

    public function run() {
        $user = model_UserMapper::getCurrentUser();
        if (null != $user->getId()) {
            if(empty($this->args['action'])) {
                $this->index();
            } else {
                switch ($this->args['action']) {

                    case 'index':
                        $this->index();
                        break;

                    case 'edit':
                        $this->edit();
                        break;

                    case 'delete':
                        $this->delete();
                        break;

                    case 'save':
                        $this->save();
                        break;

                    default:
                        $this->index();
                }
            }
        } else {
            $this->view->message = 'Нет доступа!';
            $this->view->setRenderTemplate("error.phtml");
        }
    }

    public function index() {
        $mapper = new model_TagMapper();
        if(empty($this->args['id'])) { //если id пустой, то отрисовывем все новости (fetchAll)

            $this->view->allModels = $mapper->fetchAll();
            $this->view->setRenderTemplate("tags/indexFetchAll.phtml");
        }
    }


    public function edit() {
        $user = model_UserMapper::getCurrentUser();
        if ('admin' == $user->getRole()) {
            $mapper = new model_TagMapper();
            if(!empty($this->args['id'])) {
                $this->view->oneModel = $mapper->find($this->args['id']);

            }
            $this->view->setRenderTemplate("tags/edit.phtml");
        } else {
            $this->view->message = 'Нет доступа!';
            $this->view->link = '<p><a href="index.php?controller=tag">К тегам</a></p>';
            $this->view->setRenderTemplate("error.phtml");
            return;
        }
    }

    public function delete() {
        $user = model_UserMapper::getCurrentUser();
        if ('admin' == $user->getRole()) {
            if(!empty($this->args['id'])) {
                $mapper = new model_TagMapper();
                $message = $mapper->find($this->args['id']);
                $mapper->delete($this->args['id']);
                $this->view->link = 'index.php?controller=tag';
                $this->view->setRenderTemplate("redirect.phtml");
            }  else {
                $this->view->message = 'Ошибка запроса';
                $this->view->setRenderTemplate("error.phtml");
            }
        } else {
            $this->view->message = 'Нет доступа!';
            $this->view->setRenderTemplate("error.phtml");
            return;
        }
    }

    public function save() {
        if(!empty ($_POST)) {
            $mapper = new model_TagMapper();
            $post = model_ConfigMapper::validate($_POST);

            $user = model_UserMapper::getCurrentUser();
            $post['author'] = $user->getUsername();
            // контроль, что бы Пользователь не мог писать сообщения от чужого имени,
            // меняя значения Автор в пост запросе

            $model = new model_TagModel($post);
            $mapper->save($model);

            $this->view->link = 'index.php?controller=tag&action=index';
            $this->view->setRenderTemplate("redirect.phtml");
            return;

        } else {
            $this->view->message = 'Ошибка запроса';
            $this->view->setRenderTemplate("error.phtml");
            return;
        }
    }
}